package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.BaseAdapter;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CropInfoAdapter;
import com.isunland.managesystem.base.BaseNetworkDialogFragment;
import com.isunland.managesystem.base.BaseSearchListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerDialog;
import com.isunland.managesystem.entity.SimpleListParams;
import com.isunland.managesystem.entity.rProductsListMain;
import com.isunland.managesystem.entity.rProductsListMainListOriginal;
import com.isunland.managesystem.utils.MyStringUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import java.util.ArrayList;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/4/5 16:52
 */

public class CropInfoSearchListFragment extends BaseSearchListFragment<rProductsListMain>
    implements CropInfoAdapter.Callback {
  public static final int REQUEST_CMATERIALKIND = 1;

  @Override protected BaseAdapter initAdapter(BaseVolleyActivity context, ArrayList list) {
    //noinspection unchecked
    return new CropInfoAdapter(mActivity, mList, this);
  }

  @Override protected SimpleListParams initParams() {
    SimpleListParams params = new SimpleListParams();
    //现在还没有农资详情
    params.setClassDetailActivity(null);
    params.setClassOriginal(rProductsListMainListOriginal.class);

    params.setParamsNotEmpty(
        new ParamsNotEmpty().put("materialKindCode", "").put("type", "workTask"));
    params.setTitle(MyStringUtil.connect(R.string.select, R.string.cropInfoTypeName));
    params.setUrl(ApiConst.URL_QUERY_LIST_R_PRODUCTS_LIST_MAIN);
    params.setDataStaus(mBaseParams.getDataStatus());
    return params;
  }

  @Override protected String[] setQueryField() {
    return new String[] { "productName", "mtype", "materialKindCom" };
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_query, menu);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (R.id.menu_item_query == item.getItemId()) {
      DialogFragment dialog = new CMaterialKindDialogFragment();
      dialog.setTargetFragment(this, REQUEST_CMATERIALKIND);
      dialog.show(mActivity.getSupportFragmentManager(), "");
      return true;
    }

    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode != Activity.RESULT_OK) return;

    if (requestCode == REQUEST_CMATERIALKIND && data != null) {
      CustomerDialog dialog =
          (CustomerDialog) data.getSerializableExtra(BaseNetworkDialogFragment.EXTRA_VALUE);
      getmParamsNotEmpty().put("materialKindCode", dialog.getId());
      volleyPost();
    }
  }

  @Override public void click(View v, rProductsListMain item) {
    select(item);
  }
}
